package controller

import (
	"fmt"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
	"io"
	"mime/multipart"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Upload struct{}

// 文件分片上传

func (*Upload) UploadMultiFile(c *gin.Context) {
	var file multipart.File //分片时接受的文件切片
	var header *multipart.FileHeader

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	pid, err := strconv.Atoi(c.DefaultQuery("pid", "0"))

	if err != nil {
		utils.FailWithMessage("无效的目录", c)
		return
	}

	fileType := c.Request.FormValue("type")
	fileMD5 := c.Request.FormValue("fileMD5")
	fileName := c.Request.FormValue("fileName")
	ossFilename := c.Request.FormValue("ossFilename")
	index, _ := strconv.Atoi(c.Request.FormValue("index"))
	totalSize, _ := strconv.Atoi(c.Request.FormValue("totalSize"))
	chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
	if chunkTotal != 0 {
		file, header, err = c.Request.FormFile("file")
		if err != nil {
			utils.FailWithMessage("文件接受失败", c)
			return
		}

		f, err := header.Open()
		if err != nil {
			utils.FailWithMessage("文件读取失败", c)
			return
		}

		defer func(f multipart.File) {
			err = f.Close()
			if err != nil {
				utils.FailWithMessage("文件传输失败", c)
				return
			}
		}(f)

		data, err := services.UploadService.UploadMultiFile(ossFilename, pid, claims.BaseClaims, fileMD5, fileName, chunkTotal, index, file, *header, fileType, totalSize)

		if err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		}
		data.FileName = fileName
		utils.OkWithData(data, c)

	} else {

		form, err := c.MultipartForm()

		if err != nil {
			utils.FailWithMessage("文件解析失败", c)
		}

		files := form.File["file"] //获取文件

		claims, err := tools.GetClaims(c)

		filename := c.Request.FormValue("fileName")

		if err != nil {
			utils.FailWithMessage("授权失败", c)
			c.Abort()
			return
		}

		uploadFiles, err := services.UploadService.UploadFile(int(pid), claims.BaseClaims, files, filename)

		if err != nil {
			utils.FailWithMessage("上传失败", c)
		} else {
			utils.OkWithDetailed(uploadFiles, "上传成功", c)
		}

	}

}

// 获取单个文件并且设置封面
func (*Upload) GetSingleFile(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	file, err := services.UploadService.GetSingleFile(claims.BaseClaims, id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(file, c)
}

// 多文件上传 用于上传一些图片

func (*Upload) UploadFile(c *gin.Context) {

	var pid, _ = strconv.ParseInt(c.DefaultQuery("pid", "0"), 10, 64)

	form, err := c.MultipartForm()

	if err != nil {
		utils.FailWithMessage("文件解析失败", c)
	}

	files := form.File["file"] //获取文件

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		c.Abort()
		return
	}

	uploadFiles, err := services.UploadService.UploadFile(int(pid), claims.BaseClaims, files, "")

	if err != nil {
		utils.FailWithMessage("上传失败", c)
	} else {
		utils.OkWithDetailed(uploadFiles, "上传成功", c)
	}

}

// 创建目录
func (*Upload) CreateDir(c *gin.Context) {
	var fileGroup request.FileGroup

	c.ShouldBindJSON(&fileGroup)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		c.Abort()
		return
	}

	err = services.UploadService.CreateDir(&fileGroup, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		c.Abort()
		return
	}

	utils.Ok(c)
}

func (*Upload) GetRoot(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		c.Abort()
		return
	}

	dirs, err := services.UploadService.GetRoot(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		c.Abort()
		return
	}

	utils.OkWithData(dirs, c)
}

func (*Upload) EditDir(c *gin.Context) {
	var dir request.FileGroup

	c.ShouldBindJSON(&dir)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		c.Abort()
		return
	}

	err = services.UploadService.EditDir(&dir, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		c.Abort()
		return
	}

	utils.Ok(c)
}

func (*Upload) GetFiles(c *gin.Context) {

	var paginate request.FilesListInfo

	c.ShouldBind(&paginate)

	pid, _ := strconv.ParseInt(c.DefaultQuery("id", "0"), 10, 64)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		c.Abort()
		return
	}

	data, err := services.UploadService.GetFiles(int(pid), &paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		c.Abort()
		return
	}

	utils.OkWithData(data, c)

}

// 下载单个文件
func (*Upload) UploadTempFile(c *gin.Context) {
	var fileInfo *request.TempFile

	c.ShouldBindJSON(&fileInfo)

	fmt.Println(tools.JsonParse(fileInfo))

	resp, err := http.Get(global.OC_CONFIG.AliyunOSS.BucketUrl + "/" + fileInfo.Url) //下载文件

	if err != nil {
		utils.FailWithMessage("文件转存失败", c)
		resp.Body.Close()
		return
	}

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
	}

	err = services.UploadService.UploadTempFile(claims.BaseClaims, io.Reader(resp.Body), fileInfo.ID, fileInfo.Filename)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
	}

	utils.Ok(c)
}

func (*Upload) Delete(c *gin.Context) {
	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
	}

	err = services.UploadService.Delete(claims.BaseClaims, ids.IDS)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
	}

	utils.Ok(c)
}

var UploadController = new(Upload)
